# 3.18 Breathing LED ## 3.18.1 Overview PWM breathing LED utilizes on-board programmable PWM to output analog waveform. After powering on, LED brightness can be adjusted through duty cycle of the waveform to eventually realize a breathing light. In this way, ambient light can be simulated by changing LED brightness along with time. Also, breathing LED can form a colorful mini light show to construct a tranquil and warm environment. ## 3.18.2 Working Principle PWM controls analog output via digital means, which are able to adjust the duty cycle of the wave (a signal circularly shifting between high level and low level). digital ports of voltage output are LOW and HIGH, which respectively correspond to 0V and 5V. Generally, we define LOW as 0 and HIGH as 1. will output 500 signals of 0 or 1 within 1s. If they are 500 “1”s, 5V will be output. Oppositely, if they are all 0s, the output will be 0V. Or if they are 010101010101…, the average output will be 2.5V. In other words, output ratio of 0 and 1 affects the voltage value. Honestly, it differs from real continuous output, yet the more 0 and 1 signals are output per unit time, the more accurate the control will be. ![6-18](./media/6-18-2.png) ## 3.18.2 Code Blocks 1. ![j10](./media/j10.png) This block will increase or decrease one on the value of the variable when it is execute each time. The variable name can be set. There are ++ and – – can be chosen: ++ add 1; – – minus 1. 2. ![1](./media/j11.png) This block the brightness of the LED. You need to set LED pin, channel and analog value of brightness(ranging from 0-255). ## 3.18.3 Test Code ### 3.18.3.1 Code Flow ![6-18](./media/6-18-3-1.png) ### 3.18.3.2 Build Code There are two ways to upload the code: directly open the code file we provide; or manually build blocks. **Directly open the code file we provide:** 1. Click ![](./media/j68.png) and choose `Load from your computer` ![](./media/j67.png) 2. We have already downloaded the codes on computer desktop, so open the file and choose `3-18-gradientLight.sb3` **Manually build blocks:** 1. In ![events](./media/events.png), find ![j1](./media/j1.png) block. 2. In ![control](./media/control.png), find and place ![j2](./media/j2.png) block under ![j1](./media/j1.png). ![6-1-4-1-1](./media/6-1-4-1-1.png) 3. In ![variable](./media/variable.png), drag block ![j7](./media/j7.png) (keep the default) and put it under ![j1](./media/j1.png) ![6-18](./media/6-18-3-2-1.png) 4. In ![control](./media/control.png), drag ![j6](./media/j6.png) and set times to 255, put it in ![j2](./media/j2.png) 5. In ![variable](./media/variable.png), put ![j10](./media/j10.png) in ![6-3-3-2-2](./media/6-18-3-2-2.png) ![6-18](./media/6-18-3-2-3.png) 6. In ![](./media/led.png), put ![](./media/j11.png) under ![j10](./media/j10.png) 7. In ![variable](./media/variable.png), find ![j8](./media/j8.png) and do not need to modify the name, put it in the analog value of ![](./media/j11.png). 8. Add a delay of 0.01S. ![6-18](./media/6-18-3-2-4.png) 9. Duplicate ![6-18](./media/6-18-3-2-2.png), modify ++ in ![j10](./media/j10.png) into – –. **Complete Test Code** ![6-18](./media/6-18-3-2-5.png) ### 3.18.3.3 Test Result After uploading code, the red LED gradually lights on and dims out, in a circulation. It “breathes” evenly.